// formPackage/pages/article/publishArticle/publishArticle.js
import { publishArticleAction } from "../../../../service/article_service"
import { VIDEO_URL } from "../../../../service/base_service"
import { chooseMultipleImgUploadQiniuAction, chooseAndUploadQiniuVideoAction } from "../../../../service/upload_service"
import debounce from "../../../../tools/debounce"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    topTypeIndex: 0,
    videoJoint: VIDEO_URL,
    switchArr: [
      {
        title: "发布帖子",
        key: "",
      },
      {
        title: "发布踩盘",
        key: 1
      },
    ],
    formData: {
      content: "",
      // houseNumber: "",//楼盘编号
      // title: "",//话题
      images: [

      ],
      // cityName: "成都",
    },
    showImgVideo: false,
    selectHouseName: "",
    inputNum: 0,
    houseName: "",//楼盘名

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    if (app.globalData.selectTheme) {
      this.setData({
        'formData.title': app.globalData.selectTheme
      })
      app.globalData.selectTheme = "";
    }
    if (app.globalData.articleHouse.name) {
      this.setData({
        'formData.houseNumber': app.globalData.articleHouse.houseNumber,
        selectHouseName: app.globalData.articleHouse.name
      })
      app.globalData.articleHouse = {}
    }
  },
  switchAction(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      topTypeIndex: index
    })
  },

  // 帖子内容输入绑定:
  multiBindInput(e) {
    // console.log("JJLog:e==:" + JSON.stringify(e));
    this.setData({
      'formData.content': e.detail.value,
      inputNum: e.detail.cursor
    })
  },
  //跳转选择话题
  selectThemeAction() {
    wx.navigateTo({
      url: '/formPackage/pages/article/addArticleTheme/addArticleTheme',
    })
  },

  //点击上传图片或者视频:
  pushImgVideoAction() {
    this.setData({
      showImgVideo: true
    })
  },




  popupHide() {
    this.setData({
      showImgVideo: false
    })
  },

  //确认发布帖子:
  publishArticleActions() {
    let data = this.data.formData;
    if (!data.content) {
      wx.showToast({
        title: '帖子内容不能为空?',
        icon: "none"
      })
      return;
    }
    // if (!data.houseNumber) {
    //   wx.showToast({
    //     title: '请选择帖子对应楼盘',
    //     icon: "none"
    //   })
    //   return;
    // }
    debounce(this.publishArticleRequest)


  },

  //发布帖子:
  publishArticleRequest() {
    wx.showLoading();
    console.log("JJLog:帖子内容:" + JSON.stringify(this.data.formData));
    publishArticleAction(this.data.formData).then(res => {
      wx.hideLoading();
      console.log("JJLog:发布帖子是:" + JSON.stringify(res));
      if (res.code == 200) {
        wx.navigateBack({
          delta: 0,
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || '发布失败',
        icon: "none"
      })
      console.error("JJLog:publishArticleAction", err);
    })
  },

  //选择图片发布:
  selectPushImg() {
    this.setData({
      showImgVideo: false,
    })
    let imgs = this.data.formData.images;

    chooseMultipleImgUploadQiniuAction('news').then(res => {
      console.log("JJLog:图片上传成功:" + JSON.stringify(res));
      wx.showToast({
        title: '上传成功',
      })
      res.forEach((item) => {
        let obj = {
          "fileType": 1,
          "width": item.width,
          "height": item.height,
          "url": item.complateUrl,
        }
        imgs.push(obj);
      })
      this.setData({
        'formData.images': imgs
      })
    }).catch(err => {
      console.log("JJLog:图片上传失败:" + JSON.stringify(err));
      wx.showToast({
        title: '图片上传失败',
        icon: "none"
      })
    })
  },

  //选择视频发布:
  selectPushVideo() {
    this.setData({
      showImgVideo: false,
    })
    let imgs = this.data.formData.images;
    chooseAndUploadQiniuVideoAction('news').then(imageRes => {
      console.log("JJLog:上传视频成功后返回===:" + JSON.stringify(imageRes));
      wx.showToast({
        title: '上传成功',
        icon: "none"
      })
      let obj = {
        "fileType": 2,
        "width": 0,
        "height": 0,
        "url": imageRes.complateUrl,
      }
      imgs.push(obj);
      this.setData({
        'formData.images': imgs
      })
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: '上传失败',
        icon: "none"
      })
    })
  },

  //删除一个图片或者视频:
  cancelImgsAction(e) {
    let index = e.currentTarget.dataset.index;
    let arr = this.data.formData.images;
    arr.splice(index, 1)
    this.setData({
      'formData.images': arr
    })
  },
  //跳转到选择楼盘去:
  pushToSelectHouse() {
    wx.navigateTo({
      url: '/formPackage/pages/article/selectArticleHouse/selectArticleHouse',
    })
  },


})